<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	function index(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}else{
			if($this->ion_auth->in_group("admin")){
				redirect(base_url("admin"),"refresh");
			}
			PageUtil::addVar("title",$this->config->item("site_title"));
			$this->_assign("userdata",$this->ion_auth->user_array());
			$this->_fetch('index');
		}
	}

	function login(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}else{
			PageUtil::addVar("title","เข้าสู่ระบบ - ".$this->config->item("site_title"));
			$this->_fetch("login");
		}
	}

	function checklogin(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}
		$_post = $this->input->post();

		//Check input
		if(empty($_post["username"])){
			$this->session->set_flashdata('error_message', "กรุณากรอก Username และลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");	
		}
		if(empty($_post["password"])){
			$this->session->set_flashdata('error_message', "กรุณากรอก Password และลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");	
		}
		if(!empty($_post["rememberme"])){
			$_post["rememberme"] = true;
		}else{
			$_post["rememberme"] = false;
		}
		//Check username and password are match
		$result = $this->ion_auth->login($_post["username"],$_post["password"],$_post["rememberme"]);
		if($result){
			redirect(base_url("user"),"refresh");
		}else{
			$this->session->set_flashdata('error_message', "Username และ Password ไม่ตรงกัน โปรดลองอีกครั้ง!");
			redirect(base_url("user/login"),"refresh");
		}
	}

	function register(){
		if($this->ion_auth->logged_in()){
			redirect(base_url("user"),"refresh");
		}

		$_post = $this->input->post();

		if(empty($_post["submitbtn"])){
			PageUtil::addVar("title","สมัครสมาชิก - Truck.co.th ตลาดซื้อขายรถบรรทุกและเครื่องจักรกลหนักทุกชนิด");
			$this->_fetch("register");
		}else{
			$_post["phone"] = intval($_post["phone"]);
			$_post["mobile"] = intval($_post["mobile"]);
			$username = $_post["username"];
			$password = $_post["password"];
			$email = $_post["email"];
			unset($_post["username"]);
			unset($_post["password"]);
			unset($_post["email"]);

			$result = $this->ion_auth->register($username, $password, $email, $_post);
			if(!$result){
				log_message("error","Register User failed: ".$username.", ".$password.", ".$email);
			}
			redirect(base_url("user/login"),"refresh");
		}
	}

	function ajax_checkuser(){
		if(!IS_AJAX){
			show_404();
		}

		$checkusername = $this->ion_auth->username_check($this->input->get("username"));
		if($checkusername){
			$data['responseText'] = "false";
			echo $data['responseText'];exit;
		}else{
			$data['responseText'] = "true";
			echo $data['responseText'];exit;
		}
	}
	function ajax_checkemail(){
		if(!IS_AJAX){
			show_404();
		}

		$checkusername = $this->ion_auth->email_check($this->input->get("email"));
		if($checkusername){
			$data['responseText'] = "false";
			echo $data['responseText'];exit;
		}else{
			$data['responseText'] = "true";
			echo $data['responseText'];exit;
		}
	}

	function admin_list(){
		$this->load->model("user_model","userModel");
		$datauser = $this->userModel->get();
		$this->_assign('datauser',$datauser);
		$this->_fetch("admin_list");
	}

	function edituser($id = NULL){
		$usr_id = $this->session->userdata("user_id");
		$usr_data = $this->session->userdata("user_data");		
		$this->_assign('user_data',$usr_data);
		$_post = $this->input->post();
		if(!empty($_post["submitbtn"])){
			self::register();
		}

		$this->load->model('group_model','groupModel');
		$group = $this->groupModel->get();
		$this->_assign('groups',$group);
		if(($id != NULL) AND ($usr_data['group'] == 2) ){
			$where["where"]["id"] = $id;
			$result = $this->userModel->get($id);
			$this->_assign("data",$result);
			$this->_fetch("admin_edituser");
		}else if(!empty($usr_id)){
			$where["where"]["id"] = $usr_id;
			$result = $this->userModel->get($where);
			$this->_assign("data",$result);
			$this->_fetch("admin_edituser");
		}else{
			show_404();
		}
	}

	function admin_delete($id="",$status=""){
		$usr_data = $this->session->userdata("user_data");
		$this->load->model('user_model','userModel');
		if(!empty($usr_data) AND $usr_data['group']==2){
	    if($status == 0){
				$resultuser = $this->userModel->get($id);
				$delmember['id'] = $id;
				$delmember['status'] = 1;  //staus = 1 invisible
				$this->userModel->add($delmember);
				$this->_assign('banstatus',$resultuser[0]['status']);
			}elseif($status == 1){
				$delmember1['id'] = $id;
				$delmember1['status'] = 0;  //staus = 0 Unlock
				$this->userModel->add($delmember1);
			}
			redirect('user/admin_list');
		}
	}

	function logout(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url(),"refresh");
		}
		$this->ion_auth->logout();
		redirect(base_url(),"refresh");
	}

}